import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:image_picker/image_picker.dart';
import 'package:yqys_target/gen/assets.gen.dart';
import 'package:yqys_target/utils/yq_colors.dart';

class YQUploadImages extends StatelessWidget {
  final List<String> images;
  final Function(int) onDelete;
  final Function(ImageSource source) onImagePicked;
  final int maxCount;
  final double spacing;
  final double runSpacing;
  final int crossAxisCount;
  final String title;

  const YQUploadImages({
    super.key,
    required this.images,
    required this.onDelete,
    required this.onImagePicked,
    this.maxCount = 9,
    this.spacing = 5,
    this.runSpacing = 5,
    this.crossAxisCount = 3,
    this.title = '机构图片',
  });

  void _showImagePickerActionSheet(BuildContext context) {
    showCupertinoModalPopup(
      context: context,
      builder: (BuildContext context) => CupertinoActionSheet(
        actions: <CupertinoActionSheetAction>[
          CupertinoActionSheetAction(
            onPressed: () {
              Navigator.pop(context);
              onImagePicked(ImageSource.camera);
            },
            child: const Text('拍照'),
          ),
          CupertinoActionSheetAction(
            onPressed: () {
              Navigator.pop(context);
              onImagePicked(ImageSource.gallery);
            },
            child: const Text('从相册选择'),
          ),
        ],
        cancelButton: CupertinoActionSheetAction(
          isDefaultAction: true,
          onPressed: () {
            Navigator.pop(context);
          },
          child: const Text('取消'),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              const Text(
                '*',
                style: TextStyle(color: Colors.red, fontSize: 20),
              ),
              Text(
                title,
                style: TextStyle(
                  fontSize: 15,
                  fontWeight: FontWeight.bold,
                  color: YQColors.primary,
                ),
              ),
            ],
          ),
          const SizedBox(height: 8),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(
                '请上传尽量真实的图片，最多${maxCount}张',
                style: const TextStyle(
                  fontSize: 13,
                  color: Colors.grey,
                ),
              ),
              const Expanded(child: SizedBox())
            ],
          ),
          const SizedBox(height: 16),
          Wrap(
            spacing: spacing,
            runSpacing: runSpacing,
            children: [
              ...List.generate(images.length, (index) {
                return _buildImageItem(index);
              }),
              if (images.length < maxCount) _buildAddButton(context),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildImageItem(int index) {
    return Stack(
      children: [
        Container(
          width: 100,
          height: 100,
          decoration: BoxDecoration(
            color: Colors.grey[100],
            borderRadius: BorderRadius.circular(8),
          ),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(8),
            child: Image.file(
              File(images[index]),
              fit: BoxFit.cover,
            ),
          ),
        ),
        Positioned(
          top: 4,
          right: 4,
          child: GestureDetector(
            onTap: () => onDelete(index),
            child: Container(
              padding: const EdgeInsets.all(2),
              decoration: const BoxDecoration(
                color: Colors.black45,
                shape: BoxShape.circle,
              ),
              child: const Icon(
                Icons.close,
                color: Colors.white,
                size: 16,
              ),
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildAddButton(BuildContext context) {
    return GestureDetector(
      onTap: () => _showImagePickerActionSheet(context),
      child: Container(
        width: 100,
        height: 100,
        decoration: BoxDecoration(
          color: Colors.grey[100],
          borderRadius: BorderRadius.circular(8),
        ),
        child: Image.asset(
          Assets.images.uploadPlaceholder.path,
          width: 100,
          height: 100,
          fit: BoxFit.cover,
        ),
      ),
    );
  }
}
